package cz.myslivec.slovicka.test.algorithm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import cz.myslivec.slovicka.entity.Lesson;
import cz.myslivec.slovicka.entity.Vocable;

@Name("loadingProcessor")
@Scope(ScopeType.CONVERSATION)
public class LoadingProcessor implements Serializable {
	private static final long serialVersionUID = 341139926779797237L;
	
	private static final int WANTED_SIZE = 60;

	@In
	EntityManager entityManager;

	@Logger
	Log log;
	
	@SuppressWarnings("unchecked")
	public List<Vocable> loadVocables(Lesson selectedLesson) {
		if(selectedLesson == null) {
			return new ArrayList<Vocable>();
		}
		log.info("selectedLesson: " + selectedLesson.toString());
		
		// load 'new' vocables
		Query query = entityManager.createQuery("SELECT voc FROM Vocable voc join voc.lessons ls where ls=:selectedLesson and ");
		query.setParameter("selectedLesson", selectedLesson);
		query.setParameter("nowTime", new Date());
		List<Vocable> loadedNewVocs = query.getResultList();
		Collections.shuffle(loadedNewVocs);

		// load 'in drill' vocables
		query = entityManager.createQuery("SELECT voc FROM Vocable voc join voc.lessons ls where ls=:selectedLesson");
		query.setParameter("selectedLesson", selectedLesson);
		query.setParameter("nowTime", new Date());
		List<Vocable> loadedDrilledVocs = query.getResultList();
		Collections.shuffle(loadedDrilledVocs);
		
		// load 'refreshing' vocables
		query = entityManager.createQuery("SELECT voc FROM Vocable voc join voc.lessons ls where ls=:selectedLesson");
		query.setParameter("selectedLesson", selectedLesson);
		query.setParameter("nowTime", new Date());
		List<Vocable> loadedRefreshingVocs = query.getResultList();
		Collections.shuffle(loadedRefreshingVocs);
		
		int newSize = loadedNewVocs.size();
		int drillSize = loadedDrilledVocs.size();
		int refreshSize = loadedRefreshingVocs.size();

		float xxx = (float) (WANTED_SIZE / (newSize + drillSize + refreshSize));
		
		int www1 = Math.round(xxx * newSize);
		int www2 = Math.round(xxx * drillSize);
		int www3 = Math.round(xxx * refreshSize);

		List<Vocable> result = new ArrayList<Vocable>();
		result.addAll(loadedNewVocs.subList(0, www1));
		result.addAll(loadedDrilledVocs.subList(0, www2));
		result.addAll(loadedRefreshingVocs.subList(0, www3));
		
		Collections.shuffle(result);
		
		return result;
	}
	
	private void printListVocable(List<Vocable> loadedVocs) {
		if(loadedVocs == null) {
			log.info("loadedVocs is null!!!");
			return;
		}
		if(loadedVocs.isEmpty()) {
			log.info("loadedVocs is empty!!!");
			return;
		}
		log.info("loaded list:");
		for(Vocable voc : loadedVocs) {
			log.info(voc.toString());
		}
		log.info("");
	}
}
